﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTOFrameworks;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using FooFun.DataElements.Dtos;
using FooFun.DataElements;

namespace FooFun.Web.Controls
{
    /// <summary>
    ///DataElementControl 的摘要说明
    /// </summary>
    public class DataElementControl : UserControl
    {
        public DataElementControl()
        {
            contents = new Dictionary<string, NamedContent>();
        }

#if DEBUG
        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine
                ("<!--BEGIN {0} -->", this.GetType().Name);

            base.Render(writer);

            writer.WriteLine("<!--END {0} -->", this.GetType().Name);
        }
#endif
        /*
        protected override void OnInit(EventArgs e)
        {
            Page.Trace.Warn("BeginDataElementControlInit_" + this.Parent.Parent);
            base.OnInit(e);

            contents = new Dictionary<string, NamedContent>();

            foreach (Control ctr in this.Controls)
            {
                if (ctr is NamedContent)
                {
                    NamedContent namedControl = ctr as NamedContent;
                    contents[namedControl.Name] = namedControl;
                }
            }

            foreach (Control ctr in contents.Values)
            {
                this.Controls.Remove(ctr);
            }

            this.ShowContent(NamedContent.Normal);

            Page.Trace.Warn("EndDataElementControlInit_" + this.Parent.Parent);
        }
        */
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            foreach (Control ctr in this.Controls)
            {
                if (ctr is NamedContent)
                {
                    NamedContent namedControl = ctr as NamedContent;
                    contents[namedControl.Name] = namedControl;
                }
            }

            foreach (Control ctr in contents.Values)
            {
                this.Controls.Remove(ctr);
            }

            this.ShowContent(NamedContent.Normal);
        }

        public override void DataBind()
        {
            try
            {
                base.DataBind();
            }
            catch (Exception ex)
            {
                if (ErrorContent != null)
                {
                    this.Exception = ex;

                    this.ShowContent(NamedContent.Error);

                    base.DataBind();
                }
                else
                {
                    DataElementException e
                        = new DataElementException(
                       "在" + this.Parent + "的"
                       + this.Parent.Parent + "进行数据绑定时出错", ex);
                    throw e;
                }
            }
        }

        public Exception Exception { get; set; }

        private IDataElement dataElement;

        [Bindable(BindableSupport.Yes)]
        public virtual IDataElement DataElement
        {
            get
            {
                return dataElement;
            }
            set
            {
                if ((value == null || value.IsEmpty)
                    && EmptyContent != null && NormalContent != null)
                {
                    this.ShowContent(NamedContent.Empty);
                }
                dataElement = value;

                this.DataBind();
            }
        }

        Dictionary<string, NamedContent> contents;

        public NamedContent EmptyContent
        {
            get
            {
                if (contents.ContainsKey(NamedContent.Empty))
                {
                    return contents[NamedContent.Empty];
                }
                else
                {
                    return null;
                }
            }
        }

        public NamedContent ErrorContent
        {
            get
            {
                if (contents.ContainsKey(NamedContent.Error))
                {
                    return contents[NamedContent.Error];
                }
                else
                {
                    return null;
                }
            }
        }

        public NamedContent NormalContent
        {
            get
            {
                if (contents.ContainsKey(NamedContent.Normal))
                {
                    return contents[NamedContent.Normal];
                }
                else
                {
                    return null;
                }

            }
        }

        string currentName;

        public string CurrentContentName
        {
            get { return currentName; }
        }

        public void ShowContent(string name)
        {
            if (contents.ContainsKey(name))
            {
                if (currentName != null && currentName != name)
                {
                    this.Controls.Remove(contents[currentName]);
                }

                this.currentName = name;
                this.Controls.Add(contents[currentName]);
            }
        }

        public string[] ContentNames
        {
            get
            {
                return contents.Keys.ToArray();
            }
        }

        protected override void AddParsedSubObject(object obj)
        {
            LiteralControl ltl = obj as LiteralControl;
            if (ltl != null && (ltl.Text.Trim() == string.Empty))
            {
                return;
            }
            else
            {
                base.AddParsedSubObject(obj);
            }
        }

    }
}